package agend.client.sevices;

import jade.content.lang.Codec.CodecException;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.Agent;
import jade.domain.FIPANames;
import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.domain.JADEAgentManagement.WhereIsAgentAction;
import jade.lang.acl.ACLMessage;
import agend.client.utils.GetAID;

/**
 * classe che contiene i metodi per interagire con l'AMS sul server
 */
public class AgendAMSService {

	private static final AID amsServerAid = GetAID.ams();

	
	// Chiede all'AMS remoto di trovare un agente
	/**
	 * Invia una richiesta
	 * @param sender
	 * @param aid
	 */
	public static void requestFindAgent(final Agent sender, final AID aid){

		WhereIsAgentAction whereIsAgentAction = new WhereIsAgentAction();
		whereIsAgentAction.setAgentIdentifier(aid);

		Action action = new Action();
		action.setActor(amsServerAid);
		action.setAction(whereIsAgentAction);

		ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
		msg.setSender(sender.getAID());
		msg.addReceiver(amsServerAid);
		msg.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
		msg.setOntology(JADEManagementOntology.NAME);
		msg.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);

		try {

			sender.getContentManager().registerLanguage(SLCodec.class.newInstance(), "fipa-sl");
			sender.getContentManager().registerOntology(JADEManagementOntology.getInstance());
			sender.getContentManager().fillContent(msg, action);

			sender.send(msg);

		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CodecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OntologyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
